# -*- coding: utf-8 -*-
"""
数据库工具函数
"""
import mysql.connector
from mysql.connector import Error
from .database import get_db_connection

def execute_query(query, params=None):
    """执行查询"""
    connection = get_db_connection()
    if not connection:
        return None
    
    try:
        cursor = connection.cursor(dictionary=True)
        cursor.execute(query, params or ())
        result = cursor.fetchall()
        cursor.close()
        return result
    except Error as e:
        print(f"查询执行失败: {e}")
        return None
    finally:
        if connection.is_connected():
            connection.close()

def execute_update(query, params=None):
    """执行更新操作"""
    connection = get_db_connection()
    if not connection:
        return False
    
    try:
        cursor = connection.cursor()
        cursor.execute(query, params or ())
        connection.commit()
        cursor.close()
        return True
    except Error as e:
        print(f"更新执行失败: {e}")
        connection.rollback()
        return False
    finally:
        if connection.is_connected():
            connection.close() 